/**
 * Created by chengcong on 2017/11/20.
 */
$('.form_datetime').datetimepicker({
	//language:  'fr',
	weekStart: 1,
	todayBtn: 1,
	autoclose: 1,
	todayHighlight: 1,
	startView: 2,
	forceParse: 0,
	showMeridian: 1
});
// const sokceturl = "http://47.90.123.205:8000";
$(function() {
	var msg = window.sessionStorage['user_msg'];
	msg = JSON.parse(msg);
	console.log(msg);
	socket.emit('login', {
        token:msg.token,
		username: msg.username,
		openid: null,
		sex: 0,
		marking: 1, //1pc  2wx  3app  Number
		userrole: msg.userrole //登陆角色 (医生(1)，用户(2)，助理(3)，药师(4),匿名用户(5))
	});
	$.ajax({
		url: url + '/server/assDocPending',
		type: 'post',
		data: {
			id: msg.userid,
			userRole: msg.userrole
		},
		  headers: {
          'Authorization': 'Bearer ' + msg.token
          },
		success: function(data) {
			console.log(data);
			if(data.code == 200) {
				$('table tbody').html('');
				if(data.data.length > 0) {
					for(var i = 0; i < data.data.length; i++) {
						var item=data.data[i].record.length;
						if(data.data[i].record.length>1)item=1;
						$('table tbody').append('<tr><td>' + data.data[i].name + '</td><td>' + (data.data[i].assistantName||data.data[i].serverName) + '</td>' +
							'<td>' + data.data[i].serTime + '</td><td>未处理'+['','(药师审核不通过)'][item]+'</td>' +
							'<td><button class="btn detailbtn" listid="' + data.data[i]._id + '" status="0">立即处理</button>' +
							['<button class="btn detailbtn2" userid="'+data.data[i].userid+'" listid="' + data.data[i]._id + '" status="0">拒绝</button>',''][item]+
							'</td></tr>');
					}
					$('.detailbtn').on('click', function(e) {
						console.log(1111);
						self.location = '../../postpaper/postpaper.html?detailId=' + $(e.target).attr('listid') + '&&type=' + $(e.target).attr('status');
					});
                    $('.detailbtn2').on('click', function(e) {
                        $.ajax({
                            url: url + '/server/reject',
                            type: 'get',
                            data: {
                                _id: $(e.target).attr('listid')
                            },
                            headers: {
                                'Authorization': 'Bearer ' + msg.token
                            },
                            success: function(data) {
                                console.log(data);
                                if(data.code == 200) {
                                    $(e.target).parent().parent().remove();
                                    window.top.document.getElementsByClassName('unfinish')[0].innerHTML--;
                                    // $('.unfinish').text(data.data.length)
                                    if(window.top.document.getElementsByClassName('unfinish')[0].innerHTML > 0) {
                                        window.top.document.getElementsByClassName('unfinish')[0].style.display = 'inline-block';
                                    } else {
                                        window.top.document.getElementsByClassName('unfinish')[0].style.display = 'none';
                                    };
                                
                                //拒绝处理
                                socket.emit("private_data", {
										dataType: 5,
										roomID: $(e.target).attr('userid'),
										_id: $(e.target).attr('listid'),
										text: "拒绝指派",
								});
                                //指派状态
                                $.ajax({
									type: "post",
									url: url + "/service/modification",
									async: true,
									data: {
										roomname: $(e.target).attr('userid'),
										state: "指派医生"
									},
									headers: {
										'Authorization': 'Bearer ' + msg.token
									},
									success: function(res) {
										console.log(res);
									},
									error: function(err) {
										console.log(err)
									}
								  });  
                                } else {
                                    alert(data.mes)
                                }
                            }
                        });
                    });
				} else {
					$('table tbody').append('<tr><td colspan="5"><img src="../../img/empty-b.png" alt="" style="margin-top: 200px"></td></tr>');
				}
			} else {
				alert(data.mes)
			}
		}
	});
	socket.on('inviteroom', function(data) {
		console.log(data);
		window.top.document.getElementsByClassName('invitelist')[0].innerHTML =
			parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) + 1;
		window.top.document.getElementsByClassName('invitelist')[0].style.display = 'inline-block';
        $.ajax({
            url: url + '/service/inviteroom',
            type: 'post',
            data: {
                id: msg.userid,
                roomID: data.roomID,
                time: data.time,
                username: data.username,
                state: 0,
                socket_id: data.socket_id,
                roomname: data.roomname
            },
            headers: {
                'Authorization': 'Bearer ' + msg.token
            },
            success: function (d) {
            }
        })
	})
    socket.on('private_msg', function(data) {
        console.log(data);
        window.top.document.getElementsByClassName('unfinish')[0].innerHTML =
            parseFloat(window.top.document.getElementsByClassName('unfinish')[0].innerHTML) + 1;
        window.top.document.getElementsByClassName('unfinish')[0].style.display = 'inline-block';
    })
	socket.on('inviteroomss', function(data) {
		console.log(data);
		if(data.content == false) {
			window.top.document.getElementsByClassName('invitelist')[0].innerHTML =
				parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) - 1;
			if(parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) > 0) {
				window.top.document.getElementsByClassName('invitelist')[0].style.display = 'inline-block';
			} else {
				window.top.document.getElementsByClassName('invitelist')[0].style.display = 'none';
			}
		}
	});
	socket.on('assigndoctor', function(data) {
		console.log(data);
		$.ajax({
			url: url + '/server/assDocPending',
			type: 'post',
			data: {
				id: msg.userid,
				userRole: msg.userrole
			},
		 headers: {
          'Authorization': 'Bearer ' + msg.token
          },
			success: function(data) {
				console.log(data);
				if(data.code == 200) {
					window.top.document.getElementsByClassName('unfinish')[0].innerHTML = data.data.length;
					// $('.unfinish').text(data.data.length)
					if(data.data.length > 0) {
						window.top.document.getElementsByClassName('unfinish')[0].style.display = 'inline-block';
					} else {
						window.top.document.getElementsByClassName('unfinish')[0].style.display = 'none';
					}
				} else {
					alert(data.mes)
				}
			}
		});
	});
    //审核不通过通知消息
    socket.on("notthrough_msg",function(data){
        console.log(data.msg+data._id[0]);
        var bottom=-200;
        var timer=setInterval(function () {
            bottom+=4;
            window.top.document.getElementById('msgnew').style.bottom=bottom+'px';
            window.top.document.getElementsByClassName('newmsg')[0].innerHTML='审核不通过';
            if(bottom>0){
                clearInterval(timer);
            }
        },47)
        window.top.document.getElementsByClassName('unfinish')[0].innerHTML=
            parseFloat(window.top.document.getElementsByClassName('unfinish')[0].innerHTML)+1;
        window.top.document.getElementsByClassName('unfinish')[0].style.display='inline-block';
    })
    //撤回指派
    socket.on("Undo", function(data2) {
        console.log(data2, 222)
        window.top.document.getElementsByClassName('unfinish')[0].innerHTML =
            parseFloat(window.top.document.getElementsByClassName('unfinish')[0].innerHTML) - 1;
        if(parseFloat(window.top.document.getElementsByClassName('unfinish')[0].innerHTML) > 0) {
            window.top.document.getElementsByClassName('unfinish')[0].style.display = 'inline-block';
        } else {
            window.top.document.getElementsByClassName('unfinish')[0].style.display = 'none';
        }
        alert(data2.name+"撤回指派");
        self.location='../../pages_doctor/html/unfinish.html';
    })
    //撤回邀请
    socket.on("UndoInvite", function(data) {
        window.top.document.getElementsByClassName('invitelist')[0].innerHTML =
            parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) - 1;
        if(parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) > 0) {
            window.top.document.getElementsByClassName('invitelist')[0].style.display = 'inline-block';
        } else {
            window.top.document.getElementsByClassName('invitelist')[0].style.display = 'none';
        }
    });

})
